package com.hczt.fire.server.frontend;

import com.hczt.fire.server.config.sercurity.StudentEntity;
import com.hczt.fire.server.config.sercurity.annotation.ContentSecurity;
import com.hczt.fire.server.config.sercurity.annotation.ContentSecurityAttribute;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 精诚所至，金石为开。
 * 石の上にも三年;陽気の発する所金石亦透る。
 * Faith moves mountains.
 *
 * @author marvin.ma
 * @create 2017-11-29 21:31
 * @desc 加密安全链接
 **/
@Validated
@RestController
public class SecurityController {

    @ContentSecurity
    @PostMapping(value = "/security")
    public String security(@ContentSecurityAttribute("student") @Valid StudentEntity student, String name) {
        System.out.println(com.alibaba.fastjson.JSON.toJSON(student));
        //request url:http://localhost:8082/security?desString=umvdf5P8DyFs4mEP9Jpxe/VxP0ktqYVDpBJeI2DJIoo=&name=5
        return "Successful security post with ContentSecurity";
    }

    @PostMapping(value = "/security2")
    public String security2(@RequestBody StudentEntity student) {
        System.out.println(com.alibaba.fastjson.JSON.toJSON(student));
        return "Successful security2 post without ContentSecurity";
    }

    @ContentSecurity
    @GetMapping(value = "/security3")
    public String security3(@ContentSecurityAttribute("name") @Valid @NotBlank(message = "姓名不能为空") String name,
             @ContentSecurityAttribute("age") Integer age) {
        System.out.println("controller收到的请求参数：name = " + name + ", age = " + age);
        return "Successful security3 get with ContentSecurity";
    }

    @GetMapping(value = "/security4")
    public String security4(String name, Integer age) {
        System.out.println("controller收到的请求参数：name = " + name + ", age = " + age);
        return "Successful security4 get without ContentSecurity";
    }


    @GetMapping(value = "/security5")
    public String getStudents(@ContentSecurityAttribute("name") String name) {
        System.out.println(name);
        return "Successful security5 get with ContentSecurityAttribute";
    }

    @ContentSecurity
    @GetMapping(value = "/security6/{id}")
    public String getStudents(@PathVariable Long id,
            @ContentSecurityAttribute("student") @Valid StudentEntity student,
            @Valid @NotNull(message = "职位不能为空") String position) {
        System.out.println(id);
        System.out.println(student.getName());
        System.out.println(student.getAge());
        System.out.println(position);
        //request url:http://localhost:8082/security6/14425?desString=umvdf5P8DyFs4mEP9Jpxe/VxP0ktqYVDpBJeI2DJIoo=&position=经理&age=56
        return "Successful security6 get with ContentSecurityAttribute";
    }
}
